package homework;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2023-12-03
 * Time：10:17
 */

@WebServlet("/homework.test2")
public class test2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html; charset=utf-8");
        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append("协议：" + req.getProtocol());
        stringBuilder.append("<br/>");
        stringBuilder.append("URI：" + req.getRequestURI());
        stringBuilder.append("<br/>");
        stringBuilder.append("context URI：" + req.getContextPath());
        stringBuilder.append("<br/>");
        stringBuilder.append("查询字符串：" + req.getQueryString());
        stringBuilder.append("<br/>");

        Enumeration<String> paraNames= req.getParameterNames();
        while(paraNames.hasMoreElements()) {
            String name = paraNames.nextElement();
            String value = req.getParameter(name);
            stringBuilder.append(name + "：" + value);
            stringBuilder.append("<br/>");
        }

        stringBuilder.append("<h2>header内容如下：</h2>");
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            String value = req.getHeader(name);
            stringBuilder.append(name + "：" + value);
            stringBuilder.append("<br/>");
        }

        PrintWriter printWriter = resp.getWriter();
        printWriter.write(stringBuilder.toString());
        printWriter.flush();
    }
}
